package com.kede.app.service.impl;

import com.kede.app.domain.Logistics;
import com.kede.utils.ValidationUtil;
import com.kede.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.kede.app.repository.LogisticsRepository;
import com.kede.app.service.LogisticsService;
import com.kede.app.service.dto.LogisticsDto;
import com.kede.app.service.dto.LogisticsQueryCriteria;
import com.kede.app.service.mapstruct.LogisticsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.kede.utils.PageUtil;
import com.kede.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @description 服务实现
* @author Pr.li
* @date 2023-05-10
**/
@Service
@RequiredArgsConstructor
public class LogisticsServiceImpl implements LogisticsService {

    private final LogisticsRepository logisticsRepository;
    private final LogisticsMapper logisticsMapper;

    @Override
    public Map<String,Object> queryAll(LogisticsQueryCriteria criteria, Pageable pageable){
        Page<Logistics> page = logisticsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(logisticsMapper::toDto));
    }

    @Override
    public List<LogisticsDto> queryAll(LogisticsQueryCriteria criteria){
        return logisticsMapper.toDto(logisticsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public LogisticsDto findById(String orderNo) {
        Logistics logistics = logisticsRepository.findById(orderNo).orElseGet(Logistics::new);
        ValidationUtil.isNull(logistics.getOrderNo(),"Logistics","orderNo",orderNo);
        return logisticsMapper.toDto(logistics);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LogisticsDto create(Logistics resources) {
        resources.setOrderNo(IdUtil.simpleUUID()); 
        return logisticsMapper.toDto(logisticsRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Logistics resources) {
        Logistics logistics = logisticsRepository.findById(resources.getOrderNo()).orElseGet(Logistics::new);
        ValidationUtil.isNull( logistics.getOrderNo(),"Logistics","id",resources.getOrderNo());
        logistics.copy(resources);
        logisticsRepository.save(logistics);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String orderNo : ids) {
            logisticsRepository.deleteById(orderNo);
        }
    }

    @Override
    public void download(List<LogisticsDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (LogisticsDto logistics : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("标题", logistics.getTitle());
            map.put("快递运单号", logistics.getNo());
            map.put("用户id", logistics.getUserid());
            map.put("创建时间", logistics.getCratetime());
            map.put("更新时间", logistics.getUpdatetime());
            map.put("收货地", logistics.getAddress());
            map.put("用户名 提示用", logistics.getUsername());
            map.put("说明", logistics.getRemark());
            map.put("订单状态", logistics.getStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}